﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Data;
using System.Linq;
using System.Collections;
using System.Web.Routing;
using System;
using System.Configuration;

using LuKou.Member.Domains;
using LuKou.Member.Services;
using LuKou.Web.BLL;
using LuKou.Web.BLL.Extensions;
using LuKou.Web.Controllers.Base;
using LuKou.Common.Services;
using LuKou.Web.BLL.Service;
using LuKou.Shopspace.Domains;
using LuKou.Web.Helper;
using LuKou.Web.ViewModel;

namespace LuKou.Web.Controllers.Website
{
    public class HomeController : WebBaseController
    {
        /// <summary>
        /// 首页
        /// </summary>
        public ActionResult Index(int? pageindex, int? cid, string pid, string keyword, string orderBy)
        {
            long categoryId = cid ?? 0;
            int pageIndex = pageindex ?? 1;

            int pageSize = 30;

            List<GoodsAttrib> attribs = GetProperties(pid);

            List<Goods> googsList = GoodsSearchService
                .GetGoodsList(keyword, categoryId, attribs, orderBy, pageSize * pageIndex, pageSize);

            PagedList<Goods> result = googsList
                .ToPageList<Goods>(pageIndex
                , pageSize
                , GoodsSearchService.GetGoodsCount(keyword, categoryId, attribs));

            GetCategories();
            GetPropertiesByCateId(categoryId);

            return View(result);
        }

        private void GetCategories()
        {
            long parentId;
            if (!long.TryParse(ConfigurationManager.AppSettings["CategoryId"], out parentId))
            {
                parentId = 0L;
            }
            List<Category> categoryList = CategoryService.GetCategoryList(parentId);
            TempData["CategoryList"] = categoryList;
        }


        private List<GoodsAttrib> GetProperties(string pid)
        {
            if (string.IsNullOrEmpty(pid))
            {
                return new List<GoodsAttrib>();
            }
            List<GoodsAttrib> attribs = new List<GoodsAttrib>();
            string[] properties = pid.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            string[] values;
            for (int i = 0; i < properties.Length; i++)
            {
                values = properties[i].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                attribs.Add(new GoodsAttrib()
                {
                    Pid = Convert.ToInt64(values[0]),
                    Vid = Convert.ToInt64(values[1])
                });
            }
            return attribs;
        }

        private void GetPropertiesByCateId(long cid)
        {
            if (cid <= 0)
            {
                TempData["PropertiesList"] = null;
            }
            List<CategoryPropertyValue> categoryPropertyValues = new List<CategoryPropertyValue>();
            List<CategoryProp> categoryProps = CategoryService.GetCategoryPropList(cid);
            foreach (CategoryProp data in categoryProps)
            {
                CategoryPropertyValue categoryPropertyValue = new CategoryPropertyValue();
                List<CategoryPropValue> propValues = CategoryService.GetCategoryPropValueList(cid, data.Pid);
                if (null == propValues
                    || propValues.Count > 0)
                {
                    categoryPropertyValue.PropId = data.Pid;
                    categoryPropertyValue.PropName = data.Name;
                }
                if (null != propValues 
                    && propValues.Count > 10)
                {
                    propValues = propValues.OrderByDescending(p => p.SortValue).Take(10).ToList();
                }
                propValues.ForEach(p =>
                {
                    categoryPropertyValue.PropValues.Add(new PropertyValue()
                    {
                        ValueId = p.Vid,
                        ValueName = p.Name
                    });
                });
                categoryPropertyValues.Add(categoryPropertyValue);
            }
            TempData["PropertiesList"] = categoryPropertyValues;
        }
    }
}
